package com.liyongjian.page.service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.transaction.Transactional;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.liyongjian.page.dal.ConfPageRepository;
import com.liyongjian.page.dal.ConfParamRepository;
import com.liyongjian.page.dal.ConfRepository;
import com.liyongjian.page.dal.ReportFormRepository;
import com.liyongjian.page.dal.SysRepository;
import com.liyongjian.page.dal.VisitRepository;
import com.liyongjian.page.domain.Conf;
import com.liyongjian.page.domain.ConfPage;
import com.liyongjian.page.domain.ConfParam;
import com.liyongjian.page.domain.ReportForm;
import com.liyongjian.page.domain.Sys;
import com.liyongjian.page.domain.Visit;
import com.liyongjian.page.domain.enumm.LiveDieState;
import com.liyongjian.page.domain.enumm.RepState;
import com.liyongjian.page.utils.JsonMapper;

@Component("visitService")
public class VisitService {

    private JsonMapper jsonMapper = new JsonMapper();

    public static Map<String, Object> confMap = new HashMap<>();


    public void hanldConf() {
        List<Sys> syss = sysRepository.findByState(LiveDieState.LIVE);
        for (Sys sys : syss) {
            hanldConf(sys.id);
        }
    }

    public void hanldConf(Long sysId) {
        List<Conf> confs = confRepository.findBySysIdAndState(sysId, LiveDieState.LIVE);
        for (Conf conf : confs) {
            List<ConfParam> parentParams = confParamRepository
                    .findByConfIdAndParentIdAndState(conf.id, 0l, LiveDieState.LIVE);
            for (ConfParam cp : parentParams) {
                ConfPage confPage = confPageRepository.findOne(cp.pageId);
                String key = sysId + ":" + confPage.platform + ":" + confPage.path;
                List<ConfParam> sonParams = confParamRepository
                        .findByConfIdAndParentIdAndState(conf.id, cp.id, LiveDieState.LIVE);
                Map<String, String> map = new HashMap<>();
                if (!sonParams.isEmpty() && sonParams.size() > 0) {
                    for (ConfParam sonCp : sonParams) {
                        map.put(sonCp.name, cp.name);
                    }
                    if (!confMap.containsKey(key)) {
                        List<Map<String, String>> list = new ArrayList<>();
                        list.add(map);
                        Map<String, Object> sysConfMap = new HashMap<>();
                        sysConfMap.put("list", list);
                        sysConfMap.put("conf", conf);
                        confMap.put(key, sysConfMap);
                    } else {
                        Map<String, Object> sysConfMap = (Map) confMap.get(key);
                        List<Map<String, String>> list =
                                (List<Map<String, String>>) sysConfMap.get("list");
                        list.add(map);
                        sysConfMap.put(key, list);
                        confMap.put(key, sysConfMap);
                    }
                }
            }
        }
    }

    @Autowired
    private SysRepository sysRepository;

    @Autowired
    private VisitRepository visitRepository;
    @Autowired
    private ConfRepository confRepository;
    @Autowired
    private ConfPageRepository confPageRepository;
    @Autowired
    private ConfParamRepository confParamRepository;
    @Autowired
    private ReportFormRepository reportFormRepository;

    public void page(String token, Long sysId, String platform, String path, Integer count,
            Map<String, Object> param) {
        Visit visit = new Visit();
        visit.count = count;
        visit.path = path;
        visit.platform = platform;
        visit.sysId = sysId;
        visit.token = token;
        if (!StringUtils.isEmpty(token)) {
            String[] ts = token.split("-");
            if (ts.length == 4) {
                visit.userToken = ts[3];
            }
        } else {
            visit.userToken = token;
        }
        visit.text = jsonMapper.toJson(param);
        visitRepository.save(visit);
    }


    @Transactional
    public void reportPage(Visit visit) {
        String key = visit.sysId + ":" + visit.platform + ":" + visit.path;
        if (confMap.containsKey(key)) {
            Map<String, Object> sysConfMap = (Map) confMap.get(key);
            List<Map<String, String>> lists = (List<Map<String, String>>) sysConfMap.get("list");
            Conf conf = (Conf) sysConfMap.get("conf");
            for (Map<String, String> map : lists) {
                reportPage(key, map, conf, visit);
            }
        }
        visit.repState = RepState.YCL;
        visitRepository.save(visit);
    }


    public void reportPage(String key, Map<String, String> paramConfMap, Conf conf, Visit visit) {
        Map<String, Object> textMap = jsonMapper.fromJson(visit.text, Map.class);
        for (String paramKey : paramConfMap.keySet()) {
            if (!textMap.containsKey(paramKey)) {
                return;
            }
        }

        String keys = conf.id.toString();
        for (String paramKey : paramConfMap.keySet()) {
            String value = paramConfMap.get(paramKey);
            String textValue = textMap.get(paramKey).toString();
            keys += "-" + value + ":" + textValue;
        }

        Integer count = reportFormRepository.findByKeys(keys);
        count = count == null ? visit.count : count + visit.count;
        int i = reportFormRepository.updateByKeysAndRepAt(keys, visit.createdAt, visit.count);
        if (i <= 0) {
            ReportForm rf = new ReportForm();
            rf.confId = conf.id;
            rf.count = visit.count;
            rf.addCount = visit.count;
            rf.name = keys;
            rf.repAt = visit.createdAt;
            reportFormRepository.save(rf);
        }
    }

}
